
import re
import zipfile

import gzip

import os
import json
import pymysql


def un_gz(file_name):
    # 获取文件的名称，去掉后缀名
    f_name = file_name.replace(".gz", "")
    # 开始解压
    g_file = gzip.GzipFile(file_name)
    # 读取解压后的文件，并写入去掉后缀名的同名文件（即得到解压后的文件）
    open(f_name, "wb+").write(g_file.read())
    g_file.close()

def datadist(filename):
    file = open(filename)
    count=0
    patt =r'URL：/sf/single'
    patt1 =r'------------------------------------------------------------'
    pattern = re.compile(patt)
    start = 0
    count = 0
    filename1 = 'dist_'+filename
    with open(filename1, 'a') as file_object:
        for line in file:
            result = re.search(patt, line)
            result1 = re.search(patt1, line)

            #result = pattern.findall(line)
            #print(line)
            if result is not None:
                start =1
                count = 0
            elif result1 is not None:
                start =0
                #print(line)
            if start == 1:
                count += 1

                #print(line.strip('\n'))
                if count == 5:
                    pos = line.find('[{')
                    if pos >0 :
                        #print(pos)
                        #print(line[pos:])
                        json_str = line[pos:]
                        try :
                            para_dict = json.loads(json_str, strict=False)
                            first = para_dict[0]
                            if 'fpbz' in first:
                                fpbz = first['fpbz']
                                if fpbz.find('040000') >= 0:
                                    if 'yhid' in first:
                                        if 'jsidList' in first:
                                            yhid = first['yhid']
                                            jsidlist = first['jsidList']
                                            # print(yhid)
                                            jsid = jsidlist[0]
                                            file_object.write(yhid+","+jsid+","+fpbz+"\n")
                        except ValueError:
                            print('josn error')


                    #
    file.close()



if __name__ == "__main__":
	print("edit2")
	print("master1")
	print("edit3")
	print("develop")
	print("master2")
	print("develop2")
	
	print("v0.02")
	
    rootdir = './data'
    filename ='all.log'
    list = os.listdir(rootdir)  # 列出文件夹下所有的目录与文件
    for i in range(0, len(list)):
        path = os.path.join(rootdir, list[i])
        # if os.path.isfile(path):
        # # 你想对文件的操作
        print(path)
        g_file = gzip.GzipFile(path)
        open(filename, "ab+").write(g_file.read())
    datadist(filename)


